package leecoke;

/**
 * https://leetcode.cn/problems/reshape-the-matrix/description/
 * 重塑矩阵
 */
public class Test0566 {
    public static void main(String[] args) {
        int[][] matrix = {{1, 2}, {3, 4}};
        matrixReshape(matrix, 1, 4);
    }

    public static int[][] matrixReshape(int[][] mat, int r, int c) {
        // 原始矩阵行数
        int matR = mat.length;
        // 原始矩阵列数
        int matC = mat[0].length;
        // 元素个数对不上则返回原始举证
        if (matR * matC != r * c) {
            return mat;
        }
        int[][] res = new int[r][c];
        int[] temp = new int[matR * matC];
        int k = 0;
        for (int i = 0; i < matR; i++) {
            for (int j = 0; j < matC; j++) {
                temp[k++] = mat[i][j];
            }
        }
        int index = 0;
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                res[i][j] = temp[index++];
            }
        }
        return res;
    }
}
